package com.zjitc.pigspring.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.zjitc.pigspring.bean.po.Notice;
import com.zjitc.pigspring.common.Codes;
import com.zjitc.pigspring.common.Result;
import com.zjitc.pigspring.service.NoticeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/notice")
@CrossOrigin
@Slf4j
@RestController
public class NoticeController {

    @Autowired
    private NoticeService noticeService;

    @GetMapping("/getAll")
    public Result<List<Notice>> getAll(){
        List<Notice> notices = noticeService.list();
        if (notices!=null){
            return Result.success(notices);
        }else{
            return Result.error(Codes.GET_INFO_FAILED);
        }
    }

    @GetMapping("/selectOne/{id}")
    public Result<Notice> selectNoticeById(@PathVariable Integer id){
        Notice notice = noticeService.getById(id);
        if(notice!=null){
            return Result.success(notice);
        }else{
            return Result.error(Codes.GET_INFO_FAILED);
        }
    }

    @PostMapping("/addOne")
    public Result addNotice(@RequestBody Notice notice){
        QueryWrapper<Notice> wrapper = new QueryWrapper<Notice>()
                .eq("content",notice.getContent());
        Notice one = noticeService.getOne(wrapper);
        if(one!=null){
            return Result.error(Codes.NOTICE_EXISTS);
        }
        boolean save = noticeService.save(notice);
        if(save){
            return Result.success(null, Codes.ADD_YES);
        }else{
            return Result.error(Codes.ADD_NO);
        }
    }

    @DeleteMapping("/deleteOne/{id}")
    public Result deleteNotice(@PathVariable Integer id){
        boolean flag = noticeService.removeById(id);
        if (flag){
            return Result.success(null,Codes.DELETE_YES);
        }else{
            return Result.error(Codes.DELETE_NO);
        }
    }

    @PostMapping("/deleteAll")
    public Result deleteAllNotice(@RequestBody List<Integer> ids){

        boolean flag = noticeService.removeBatchByIds(ids);
        if(flag){
            return Result.success(null,Codes.DELETE_YES);
        }else{
            return Result.error(Codes.DELETE_NO);
        }
    }

    @PutMapping("/updateOne")
    public Result updateNotice(@RequestBody Notice notice){
        QueryWrapper<Notice> wrapper = new QueryWrapper<Notice>()
                .eq("content",notice.getContent());
        Notice one = noticeService.getOne(wrapper);
        if(one!=null && one.getContent().equals(notice.getContent())){
            return Result.error(Codes.NOTICE_EXISTS);
        }
        boolean flag = noticeService.updateById(notice);
        if(flag){
            return Result.success(Codes.UPDATE_YES);
        }else{
            return Result.error(Codes.UPDATE_NO);
        }
    }

    @PostMapping("/conditionQuery")
    public Result<List<Notice>> queryNotice(@RequestBody Notice notice){

        QueryWrapper<Notice> queryWrapper = new QueryWrapper<Notice>()
                .like(StringUtils.isNotBlank(notice.getContent()),"content",notice.getContent());
        List<Notice> list = noticeService.list(queryWrapper);
        if(list!=null){
            return Result.success(list);
        }else{
            return Result.error(Codes.GET_INFO_FAILED);
        }
    }
}
